//
//  ViewController.m
//  21-NSURLRequestAndResponse
//
//  Created by GivenBmli on 16/8/11.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    NSURL 负责请求地址
   NSURL *baiduURL = [NSURL URLWithString:@"https://www.baidu.com/user/wanghuang/1.html?userName=xxx&pwd=xxx"];
    
    NSLog(@"baiduURL:%@",baiduURL);
//    1.完整的URL对应的字符串
    NSLog(@"baiduURL.absoluteString:%@",baiduURL.absoluteString);
//    2.host :主机地址/域名
    NSLog(@"baiduURL.host:%@",baiduURL.host);
//    3.path:主机地址/域名后对应的路径
    NSLog(@"baiduURL.path:%@",baiduURL.path);
//    4.query:?以后的内容
    NSLog(@"baiduURL.query:%@",baiduURL.query);
//    5.scheme:所遵遵循的协议
    NSLog(@"baiduURL.scheme:%@",baiduURL.scheme);
//    6.resourceSpecifier:除了协议的部分
    NSLog(@"baiduURL.resourceSpecifier:%@",baiduURL.resourceSpecifier);
    
//    不可变请求,和可变请求
//    1.NSURLRequest
   NSURLRequest *daiduRequest = [NSURLRequest requestWithURL:baiduURL];
//    2.NSMutableURLRequest
//    一般处理网路请求,都使用NSMutableURLRequest
  NSMutableURLRequest *baiduMutableRequest = [NSMutableURLRequest requestWithURL:baiduURL];
//    1.>超时时间,一个请求持续多久,会失败
    baiduMutableRequest.timeoutInterval = 60;
//    2.>HTTPMethod 请求的姿势 GET POST PUT DELETE
    baiduMutableRequest.HTTPMethod = @"GET";
//    3.>allHTTPHeaderFields 设置,网络请求头;
    baiduMutableRequest.allHTTPHeaderFields = @{@"User-Agent":@"iOS 9.3 / iPhone8,1"};
//    根据字典特性,相同key情况下,后填入的值,会覆盖前面的值
    [baiduMutableRequest setValue:@"iOS 9.3 / iPhone8,1" forHTTPHeaderField:@"User-Agent"];
    NSLog(@"%@",baiduMutableRequest.allHTTPHeaderFields);
//    4>HTTPBody (一般POST请求时,才会使用)
//    一般情况下都是字典/字符串转换成的Data
    baiduMutableRequest.HTTPBody = [NSData data];
//    3.NSURLResponse 响应
//    NSURLResponse
    NSHTTPURLResponse *resPonse =  [[NSHTTPURLResponse alloc]initWithURL:baiduURL statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:@{@"date":[NSDate date]}];
//   1> statusCode 状态码
//    resPonse.statusCode
//    2>allHeaderFields 响应头
//    内容格式 编码
//    Content-Type: aoolicatin/json;charser:utf-8
//    时间 日期
//    缓存的一些内容
    NSLog(@"%@",resPonse.allHeaderFields);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
